import request from '@/utils/request'

// ==================== 文档管理 ====================

// 获取文档列表
export function getDocumentList(params) {
  return request({
    url: '/api/zenithMind/document/list',
    method: 'get',
    params
  })
}

// 获取文档详情
export function getDocumentDetail(id) {
  return request({
    url: `/api/zenithMind/document/${id}`,
    method: 'get'
  })
}

// 创建文档
export function createDocument(data) {
  return request({
    url: '/api/zenithMind/document',
    method: 'post',
    data
  })
}

// 更新文档
export function updateDocument(id, data) {
  return request({
    url: `/api/zenithMind/document/${id}`,
    method: 'put',
    data
  })
}

// 删除文档
export function deleteDocument(id) {
  return request({
    url: `/api/zenithMind/document/${id}`,
    method: 'delete'
  })
}

// 搜索文档
export function searchDocuments(keyword, params) {
  return request({
    url: '/api/zenithMind/document/search',
    method: 'get',
    params: { keyword, ...params }
  })
}

// 获取我的文档
export function getMyDocuments(params) {
  return request({
    url: '/api/zenithMind/document/my',
    method: 'get',
    params
  })
}

// 获取最近编辑的文档
export function getRecentDocuments(limit = 10) {
  return request({
    url: '/api/zenithMind/document/recent',
    method: 'get',
    params: { limit }
  })
}

// 收藏文档
export function favoriteDocument(id) {
  return request({
    url: `/api/zenithMind/document/${id}/favorite`,
    method: 'post'
  })
}

// 取消收藏
export function unfavoriteDocument(id) {
  return request({
    url: `/api/zenithMind/document/${id}/unfavorite`,
    method: 'post'
  })
}

// 获取收藏文档
export function getFavoriteDocuments(params) {
  return request({
    url: '/api/zenithMind/document/favorites',
    method: 'get',
    params
  })
}

// 获取文档模板
export function getDocumentTemplates() {
  return request({
    url: '/api/zenithMind/document/templates',
    method: 'get'
  })
}

// 获取文档版本历史
export function getDocumentVersions(documentId) {
  return request({
    url: `/api/zenithMind/document/${documentId}/versions`,
    method: 'get'
  })
}

// 恢复文档版本
export function restoreDocumentVersion(documentId, versionId) {
  return request({
    url: `/api/zenithMind/document/${documentId}/versions/${versionId}/restore`,
    method: 'post'
  })
}

// 获取相关文档
export function getRelatedDocuments(documentId) {
  return request({
    url: `/api/zenithMind/document/${documentId}/related`,
    method: 'get'
  })
}

// 获取文档评论
export function getDocumentComments(documentId) {
  return request({
    url: `/api/zenithMind/document/${documentId}/comments`,
    method: 'get'
  })
}

// 添加文档评论
export function addDocumentComment(data) {
  return request({
    url: '/api/zenithMind/document/comments',
    method: 'post',
    data
  })
}

// ==================== 文档分类管理 ====================

// 获取文档分类
export function getDocumentCategories() {
  return request({
    url: '/api/zenithMind/document/categories',
    method: 'get'
  })
}

// 创建文档分类
export function createDocumentCategory(data) {
  return request({
    url: '/api/zenithMind/document/categories',
    method: 'post',
    data
  })
}

// 更新文档分类
export function updateDocumentCategory(id, data) {
  return request({
    url: `/api/zenithMind/document/categories/${id}`,
    method: 'put',
    data
  })
}

// 删除文档分类
export function deleteDocumentCategory(id) {
  return request({
    url: `/api/zenithMind/document/categories/${id}`,
    method: 'delete'
  })
}

// 按分类获取文档
export function getDocumentsByCategory(categoryId, params) {
  return request({
    url: `/api/zenithMind/document/category/${categoryId}`,
    method: 'get',
    params
  })
}

// ==================== 文档分享管理 ====================

// 分享文档
export function shareDocument(id, data) {
  return request({
    url: `/api/zenithMind/document/${id}/share`,
    method: 'post',
    data
  })
}

// 取消分享
export function unshareDocument(id) {
  return request({
    url: `/api/zenithMind/document/${id}/unshare`,
    method: 'post'
  })
}

// 获取分享的文档
export function getSharedDocuments(params) {
  return request({
    url: '/api/zenithMind/document/shared',
    method: 'get',
    params
  })
}

// 获取我分享的文档
export function getMySharedDocuments(params) {
  return request({
    url: '/api/zenithMind/document/my-shared',
    method: 'get',
    params
  })
}

// 访问分享文档
export function accessSharedDocument(shareCode, password) {
  return request({
    url: `/api/zenithMind/document/shared/${shareCode}`,
    method: 'post',
    data: { password }
  })
}

// ==================== 文档协作管理 ====================

// 邀请协作者
export function inviteDocumentCollaborator(documentId, data) {
  return request({
    url: `/api/zenithMind/document/${documentId}/collaborators`,
    method: 'post',
    data
  })
}

// 获取协作者列表
export function getDocumentCollaborators(documentId) {
  return request({
    url: `/api/zenithMind/document/${documentId}/collaborators`,
    method: 'get'
  })
}

// 移除协作者
export function removeDocumentCollaborator(documentId, userId) {
  return request({
    url: `/api/zenithMind/document/${documentId}/collaborators/${userId}`,
    method: 'delete'
  })
}

// 更新协作权限
export function updateDocumentCollaboratorPermission(documentId, userId, permission) {
  return request({
    url: `/api/zenithMind/document/${documentId}/collaborators/${userId}/permission`,
    method: 'put',
    data: { permission }
  })
}

// 获取协作文档
export function getCollaborativeDocuments(params) {
  return request({
    url: '/api/zenithMind/document/collaborative',
    method: 'get',
    params
  })
}

// ==================== 文档版本管理 ====================

// 获取文档版本历史
export function getDocumentVersions(documentId) {
  return request({
    url: `/api/zenithMind/document/${documentId}/versions`,
    method: 'get'
  })
}

// 创建文档版本
export function createDocumentVersion(documentId, data) {
  return request({
    url: `/api/zenithMind/document/${documentId}/versions`,
    method: 'post',
    data
  })
}

// 恢复文档版本
export function restoreDocumentVersion(documentId, versionId) {
  return request({
    url: `/api/zenithMind/document/${documentId}/versions/${versionId}/restore`,
    method: 'post'
  })
}

// 比较文档版本
export function compareDocumentVersions(documentId, version1, version2) {
  return request({
    url: `/api/zenithMind/document/${documentId}/versions/compare`,
    method: 'get',
    params: { version1, version2 }
  })
}

// 删除文档版本
export function deleteDocumentVersion(documentId, versionId) {
  return request({
    url: `/api/zenithMind/document/${documentId}/versions/${versionId}`,
    method: 'delete'
  })
}

// ==================== 文档模板管理 ====================

// 获取文档模板
export function getDocumentTemplates(params) {
  return request({
    url: '/api/zenithMind/document/templates',
    method: 'get',
    params
  })
}

// 使用模板创建文档
export function createDocumentFromTemplate(templateId, data) {
  return request({
    url: `/api/zenithMind/document/templates/${templateId}/create`,
    method: 'post',
    data
  })
}

// 保存为模板
export function saveDocumentAsTemplate(documentId, data) {
  return request({
    url: `/api/zenithMind/document/${documentId}/save-as-template`,
    method: 'post',
    data
  })
}

// ==================== 文档评论管理 ====================

// 获取文档评论
export function getDocumentComments(documentId, params) {
  return request({
    url: `/api/zenithMind/document/${documentId}/comments`,
    method: 'get',
    params
  })
}

// 添加文档评论
export function addDocumentComment(documentId, data) {
  return request({
    url: `/api/zenithMind/document/${documentId}/comments`,
    method: 'post',
    data
  })
}

// 回复文档评论
export function replyDocumentComment(commentId, data) {
  return request({
    url: `/api/zenithMind/document/comments/${commentId}/reply`,
    method: 'post',
    data
  })
}

// 删除文档评论
export function deleteDocumentComment(commentId) {
  return request({
    url: `/api/zenithMind/document/comments/${commentId}`,
    method: 'delete'
  })
}

// ==================== 文档上传 ====================

// 上传文档图片
export function uploadDocumentImage(formData) {
  return request({
    url: '/api/zenithMind/document/upload/image',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 上传文档附件
export function uploadDocumentAttachment(formData) {
  return request({
    url: '/api/zenithMind/document/upload/attachment',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// ==================== 文档导入导出 ====================

// 导出文档
export function exportDocument(documentId, format) {
  return request({
    url: `/api/zenithMind/document/${documentId}/export`,
    method: 'get',
    params: { format },
    responseType: 'blob'
  })
}

// 导入文档
export function importDocument(formData) {
  return request({
    url: '/api/zenithMind/document/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}
